/*
Hermes Core: General Purpose Mobile Agent Platform
Copyright (C) 2006 UNICAM - Università degli Studi di Camerino

Group leaders: Flavio Corradini and Emanuela Merelli

Developers: Ezio Bartocci, Diego Bonura, Rosario Culmone,
Davide Lorenzoli, Leonardo Mariani, Francesca Piersigilli, 
Lorenzo Scortichini, Leonardo Vito.

GNU Lesser Public License

This library is free software; you can redistribute it 
and/or modify it under the terms of the GNU Lesser General 
Public License as published by the Free Software Foundation, 
version 2.1 of the License.

This library is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public 
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

Emanuela Merelli
Dipartimento di Matematica ed Informatica
Università di Camerino
via Madonna delle Carceri, 62032 Camerino, Italy
E.Mail: emanuela.merelli@unicam.it
*/
package hermesV2.basicServices.communication;

import hermesV2.PlaceAddress;
import hermesV2.agent.Identificator;
import hermesV2.util.HermesLogger;

/**
 * Handles lost message usually sent during the migration of an agent.
 * It has low priority and a sleep commands.
 * It has simulated by a special system pseudo-agent with the reception enaled.
 * @author diegobonura
 *
 */
public class LazyMessageHandle extends Thread {
    
    private CommunicationManager cm;

    private Identificator lazyId;

    /**
     * The constructor of the thread that handles lost messages.
     * @param cm The Communication Manager reference.
     * @param curerntPlaceAddress The place address of the running platform
     */
    public LazyMessageHandle(CommunicationManager cm,
            PlaceAddress curerntPlaceAddress) {
        this.cm = cm;
        lazyId = new Identificator(curerntPlaceAddress, "LazyMessageHandle",
                false);
        cm.setAgentReception(lazyId);
    }

    /**
     * Returns the identificator of the handle message threads.
     * 
     * @return The identificator of the handle thread.
     */
    protected Identificator getLazyMessageHandleId() {
        return lazyId;

    }

    /**
     * The run of the thread.
     */
    public void run() {
        while (true) {
            try{
            cm.sendMessageToUserAgent(cm.getMessageSynch(lazyId));
            }catch(SendFailedException sfe){}
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ie) {
                HermesLogger.log(HermesLogger.WARNING,
                        "Lazy message handle errore", ie);
            }
        }

    }
}